package com.dxyxx.framework;

import java.util.Date;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import br.com.pierry.simpletoast.SimpleToast;

import com.dxyxx.R;
import com.dxyxx.android.setting.UserPreferences;
import com.dxyxx.dialog.ViewTouchListener;
import com.dxyxx.pojo.Ticket;

/**
 * 为了代金券 准备的pay2activity
 * 
 * @author Administrator
 * 
 */
public class Pay2Activity extends BasePayActivity {
	private Ticket order;
	private ProgressDialog progressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pay);

		// 返回首页 动态效果。
		TextView backToHome = (TextView) findViewById(R.id.backToHome);
		backToHome.setOnTouchListener(new ViewTouchListener(Pay2Activity.this, backToHome));
		backToHome.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				startActivity(new Intent(Pay2Activity.this, FragmentMainActivity.class));
			}
		});
		//		ViewGroup rootViewGroup = (ViewGroup) findViewById(R.id.content);
		Bundle bundle = this.getIntent().getExtras();
		order = (Ticket) bundle.getSerializable("ticket");

		initViews();

	}

	public void initViews() {
		((ImageView) findViewById(R.id.backImageButton)).setVisibility(View.GONE);
		((ImageView) findViewById(R.id.nextImageButton)).setVisibility(View.INVISIBLE);
		TextView title = (TextView) findViewById(R.id.title);
		TextView orderMoney = (TextView) findViewById(R.id.orderMoney);
		TextView orderDeliveryType = (TextView) findViewById(R.id.orderDeliveryType);
		TextView orderId = (TextView) findViewById(R.id.orderId);
		ImageView et_payType = (ImageView) findViewById(R.id.payIcon);

		orderMoney.setText("￥" + df.format(order.getMoney()));
		orderDeliveryType.setText("当天");
		orderId.setText(order.getUuid());
		if (order.getPayType().equals("alipay")) {
			et_payType.setImageResource(R.drawable.alipay_icon);
		} else {
			et_payType.setImageResource(R.drawable.wechat);
		}

		title.setText("订单提交成功");
		title.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);

		findViewById(R.id.payButton).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (!jisuan(order.getPostTime(), new Date())) {
					SimpleToast.error(Pay2Activity.this, "订单已经超过24小时，无效了");
					return;
				}
				if (order.getPayType().equals("tenpay")) {
					userPreferences = UserPreferences.getInstance(getApplicationContext());
					userPreferences.setPayType("t");
					progressDialog = ProgressDialog.show(Pay2Activity.this, null, null);
					progressDialog.setContentView(R.layout.dialog_circle);
					tenPay(Pay2Activity.this, progressDialog, order.getUuid());
				} else if (order.getPayType().equals("alipay")) {
					alipay(order.getUuid(),order.getMoney());
				} else {
					//...
				}
			}
		});

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (data != null) {

		}
	}

}
